﻿using System;
using UnityEngine;

namespace RenderHeads.Media.AVProVideo
{
	// Token: 0x0200001B RID: 27
	[AddComponentMenu("AVPro Video/Cubemap Cube (VR)", 400)]
	[RequireComponent(typeof(MeshFilter))]
	[RequireComponent(typeof(MeshRenderer))]
	[HelpURL("http://renderheads.com/product/avpro-video/")]
	public class CubemapCube : MonoBehaviour
	{
		// Token: 0x1700000F RID: 15
		// (get) Token: 0x060000A0 RID: 160 RVA: 0x00006570 File Offset: 0x00004770
		// (set) Token: 0x0600009F RID: 159 RVA: 0x00006564 File Offset: 0x00004764
		public MediaPlayer Player
		{
			get
			{
				return this._mediaPlayer;
			}
			set
			{
				this._mediaPlayer = value;
			}
		}

		// Token: 0x060000A1 RID: 161 RVA: 0x00006578 File Offset: 0x00004778
		private void Awake()
		{
			if (CubemapCube._propApplyGamma == 0)
			{
				CubemapCube._propApplyGamma = Shader.PropertyToID("_ApplyGamma");
			}
			if (CubemapCube._propUseYpCbCr == 0)
			{
				CubemapCube._propUseYpCbCr = Shader.PropertyToID("_UseYpCbCr");
			}
			if (CubemapCube._propChromaTex == 0)
			{
				CubemapCube._propChromaTex = Shader.PropertyToID("_ChromaTex");
			}
		}

		// Token: 0x060000A2 RID: 162 RVA: 0x000065D0 File Offset: 0x000047D0
		private void Start()
		{
			if (this._mesh == null)
			{
				this._mesh = new Mesh();
				this._mesh.MarkDynamic();
				MeshFilter component = base.GetComponent<MeshFilter>();
				if (component != null)
				{
					component.mesh = this._mesh;
				}
				this._renderer = base.GetComponent<MeshRenderer>();
				if (this._renderer != null)
				{
					this._renderer.material = this._material;
				}
				this.BuildMesh();
			}
		}

		// Token: 0x060000A3 RID: 163 RVA: 0x00006658 File Offset: 0x00004858
		private void OnDestroy()
		{
			if (this._mesh != null)
			{
				MeshFilter component = base.GetComponent<MeshFilter>();
				if (component != null)
				{
					component.mesh = null;
				}
				UnityEngine.Object.Destroy(this._mesh);
				this._mesh = null;
			}
			if (this._renderer != null)
			{
				this._renderer.material = null;
				this._renderer = null;
			}
		}

		// Token: 0x060000A4 RID: 164 RVA: 0x000066C8 File Offset: 0x000048C8
		private void LateUpdate()
		{
			if (Application.isPlaying)
			{
				if (this._mediaPlayer != null && this._mediaPlayer.Control != null)
				{
					if (this._mediaPlayer.TextureProducer != null)
					{
						Texture texture = this._mediaPlayer.TextureProducer.GetTexture(0);
						bool flag = this._mediaPlayer.TextureProducer.RequiresVerticalFlip();
						if (this._texture != texture || this._verticalFlip != flag || (texture != null && (this._textureWidth != texture.width || this._textureHeight != texture.height)))
						{
							this._texture = texture;
							if (texture != null)
							{
								this.UpdateMeshUV(texture.width, texture.height, flag);
							}
						}
						if (this._renderer.material.HasProperty(CubemapCube._propApplyGamma) && this._mediaPlayer.Info != null)
						{
							Helper.SetupGammaMaterial(this._renderer.material, this._mediaPlayer.Info.PlayerSupportsLinearColorSpace());
						}
						if (this._renderer.material.HasProperty(CubemapCube._propUseYpCbCr) && this._mediaPlayer.TextureProducer.GetTextureCount() == 2)
						{
							this._renderer.material.EnableKeyword("USE_YPCBCR");
							this._renderer.material.SetTexture(CubemapCube._propChromaTex, this._mediaPlayer.TextureProducer.GetTexture(1));
						}
					}
					this._renderer.material.mainTexture = this._texture;
				}
				else
				{
					this._renderer.material.mainTexture = null;
				}
			}
		}

		// Token: 0x060000A5 RID: 165 RVA: 0x0000688C File Offset: 0x00004A8C
		private void BuildMesh()
		{
			Vector3 b = new Vector3(-0.5f, -0.5f, -0.5f);
			Vector3[] array = new Vector3[]
			{
				new Vector3(0f, -1f, 0f) - b,
				new Vector3(0f, 0f, 0f) - b,
				new Vector3(0f, 0f, -1f) - b,
				new Vector3(0f, -1f, -1f) - b,
				new Vector3(0f, 0f, 0f) - b,
				new Vector3(-1f, 0f, 0f) - b,
				new Vector3(-1f, 0f, -1f) - b,
				new Vector3(0f, 0f, -1f) - b,
				new Vector3(-1f, 0f, 0f) - b,
				new Vector3(-1f, -1f, 0f) - b,
				new Vector3(-1f, -1f, -1f) - b,
				new Vector3(-1f, 0f, -1f) - b,
				new Vector3(-1f, -1f, 0f) - b,
				new Vector3(0f, -1f, 0f) - b,
				new Vector3(0f, -1f, -1f) - b,
				new Vector3(-1f, -1f, -1f) - b,
				new Vector3(0f, -1f, -1f) - b,
				new Vector3(0f, 0f, -1f) - b,
				new Vector3(-1f, 0f, -1f) - b,
				new Vector3(-1f, -1f, -1f) - b,
				new Vector3(-1f, -1f, 0f) - b,
				new Vector3(-1f, 0f, 0f) - b,
				new Vector3(0f, 0f, 0f) - b,
				new Vector3(0f, -1f, 0f) - b
			};
			Matrix4x4 matrix4x = Matrix4x4.TRS(Vector3.zero, Quaternion.AngleAxis(-90f, Vector3.right), Vector3.one);
			for (int i = 0; i < array.Length; i++)
			{
				array[i] = matrix4x.MultiplyPoint(array[i]);
			}
			this._mesh.vertices = array;
			this._mesh.triangles = new int[]
			{
				0,
				1,
				2,
				0,
				2,
				3,
				4,
				5,
				6,
				4,
				6,
				7,
				8,
				9,
				10,
				8,
				10,
				11,
				12,
				13,
				14,
				12,
				14,
				15,
				16,
				17,
				18,
				16,
				18,
				19,
				20,
				21,
				22,
				20,
				22,
				23
			};
			this._mesh.normals = new Vector3[]
			{
				new Vector3(-1f, 0f, 0f),
				new Vector3(-1f, 0f, 0f),
				new Vector3(-1f, 0f, 0f),
				new Vector3(-1f, 0f, 0f),
				new Vector3(0f, -1f, 0f),
				new Vector3(0f, -1f, 0f),
				new Vector3(0f, -1f, 0f),
				new Vector3(0f, -1f, 0f),
				new Vector3(1f, 0f, 0f),
				new Vector3(1f, 0f, 0f),
				new Vector3(1f, 0f, 0f),
				new Vector3(1f, 0f, 0f),
				new Vector3(0f, 1f, 0f),
				new Vector3(0f, 1f, 0f),
				new Vector3(0f, 1f, 0f),
				new Vector3(0f, 1f, 0f),
				new Vector3(0f, 0f, 1f),
				new Vector3(0f, 0f, 1f),
				new Vector3(0f, 0f, 1f),
				new Vector3(0f, 0f, 1f),
				new Vector3(0f, 0f, -1f),
				new Vector3(0f, 0f, -1f),
				new Vector3(0f, 0f, -1f),
				new Vector3(0f, 0f, -1f)
			};
			this.UpdateMeshUV(512, 512, false);
		}

		// Token: 0x060000A6 RID: 166 RVA: 0x00007004 File Offset: 0x00005204
		private void UpdateMeshUV(int textureWidth, int textureHeight, bool flipY)
		{
			this._textureWidth = textureWidth;
			this._textureHeight = textureHeight;
			this._verticalFlip = flipY;
			float num = (float)textureWidth;
			float num2 = (float)textureHeight;
			float num3 = num / 3f;
			float num4 = Mathf.Floor((this.expansion_coeff * num3 - num3) / 2f);
			float num5 = num4 / num;
			float num6 = num4 / num2;
			Vector2[] array = new Vector2[]
			{
				new Vector2(0.33333334f + num5, 1f - num6),
				new Vector2(0.6666667f - num5, 1f - num6),
				new Vector2(0.6666667f - num5, 0.5f + num6),
				new Vector2(0.33333334f + num5, 0.5f + num6),
				new Vector2(0.33333334f + num5, 0.5f - num6),
				new Vector2(0.6666667f - num5, 0.5f - num6),
				new Vector2(0.6666667f - num5, num6),
				new Vector2(0.33333334f + num5, num6),
				new Vector2(num5, 1f - num6),
				new Vector2(0.33333334f - num5, 1f - num6),
				new Vector2(0.33333334f - num5, 0.5f + num6),
				new Vector2(num5, 0.5f + num6),
				new Vector2(0.6666667f + num5, 0.5f - num6),
				new Vector2(1f - num5, 0.5f - num6),
				new Vector2(1f - num5, num6),
				new Vector2(0.6666667f + num5, num6),
				new Vector2(num5, num6),
				new Vector2(num5, 0.5f - num6),
				new Vector2(0.33333334f - num5, 0.5f - num6),
				new Vector2(0.33333334f - num5, num6),
				new Vector2(1f - num5, 1f - num6),
				new Vector2(1f - num5, 0.5f + num6),
				new Vector2(0.6666667f + num5, 0.5f + num6),
				new Vector2(0.6666667f + num5, 1f - num6)
			};
			if (flipY)
			{
				for (int i = 0; i < array.Length; i++)
				{
					array[i].y = 1f - array[i].y;
				}
			}
			this._mesh.uv = array;
			this._mesh.UploadMeshData(false);
		}

		// Token: 0x04000093 RID: 147
		private Mesh _mesh;

		// Token: 0x04000094 RID: 148
		protected MeshRenderer _renderer;

		// Token: 0x04000095 RID: 149
		[SerializeField]
		protected Material _material;

		// Token: 0x04000096 RID: 150
		[SerializeField]
		private MediaPlayer _mediaPlayer;

		// Token: 0x04000097 RID: 151
		[SerializeField]
		private float expansion_coeff = 1.01f;

		// Token: 0x04000098 RID: 152
		private Texture _texture;

		// Token: 0x04000099 RID: 153
		private bool _verticalFlip;

		// Token: 0x0400009A RID: 154
		private int _textureWidth;

		// Token: 0x0400009B RID: 155
		private int _textureHeight;

		// Token: 0x0400009C RID: 156
		private static int _propApplyGamma;

		// Token: 0x0400009D RID: 157
		private static int _propUseYpCbCr;

		// Token: 0x0400009E RID: 158
		private const string PropChromaTexName = "_ChromaTex";

		// Token: 0x0400009F RID: 159
		private static int _propChromaTex;
	}
}
